use "${filedata}transactions_application_controls_2023.dta", clear

///////////////////////

drop if application_type !="REGULAR" 
drop if filing_year>2012 
keep if US_group==1 //keep only teams with all applicants in the US

gen patent_denied = (patent_issued==0)

egen art_year=group(examiner_art_unit year)
egen examiner_year=group(examiner_id year)
egen examiner_art_year=group(examiner_id examiner_art_unit year)

//following Williams
egen tag_within_art=tag(examiner_art_year)
egen sum_within_art=sum(tag_within_art), by(art_year)
drop if sum_within_art<=2 //limit to art unit- years with at least two examiners
egen count_examiner= count(1), by(examiner_art_year) 
drop if count_examiner<=10 //limit to examiners who have seen at least 10 apps in an art unit year

//create instruments 

** IV by initial rejection
egen sum_initial= sum(initial_rejection), by(art_year examiner_id )
egen count_initial= count(initial_rejection), by(art_year examiner_id)
gen iv_initial=(sum_initial-initial_rejection)/(count_initial-1)

** IV by patent denied
egen sum_denied= sum(patent_denied), by(art_year examiner_id )
egen count_denied= count(patent_denied), by(art_year examiner_id)
gen iv_final=(sum_denied-patent_denied)/(count_denied-1)

*Construction:
foreach i in half_female prop_female all_female {
	gen iv_final_`i'=iv_final*`i'
	gen iv_initial_`i'=iv_initial*`i'
	gen `i'_initial_rejection = initial_rejection * `i'
}

gen Female=.
gen iv=.
gen Female_iv=.
gen Female_initial_rejection=.

save "${filedata}for_regs.dta", replace
